package com.ymm.system.core.facade;

import com.github.pagehelper.PageHelper;
import com.ymm.driver.api.constants.Entity.DriverEntity;
import com.ymm.lulutong.dto.OrderShipperDTO;
import com.ymm.lulutong.utils.Result;
import com.ymm.system.api.constants.CargoEntity;
import com.ymm.system.api.constants.LltorderEntity;
import com.ymm.system.api.constants.ShipperEntity;
import com.ymm.system.api.constants.TradeEntity;
import com.ymm.system.api.dto.LltorderDTO;
import com.ymm.system.api.dto.TradeDTO;
import com.ymm.system.api.dto.*;
import com.ymm.system.api.facade.SystemDisplayService;
import com.ymm.system.core.repo.DriverRelatedRepo;
import com.ymm.system.core.repo.ShipperRelatedRepo;
import com.ymm.system.dao.mapper.CargoMapper;
import com.ymm.system.dao.mapper.LltorderMapper;
import com.ymm.system.dao.mapper.TradeMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther ZhangHongyuan
 * @date 2022/8/3 14:11
 */

@Component
@Service(version = "server")
public class SystemDisplayServiceImpl implements SystemDisplayService {

    @Resource
    private LltorderMapper lltorderMapper;

    @Resource
    private TradeMapper tradeMapper;

    @Resource
    private CargoMapper cargoMapper;

    @Autowired
    private ShipperRelatedRepo shipperRelatedRepo;

    @Autowired
    private DriverRelatedRepo driverRelatedRepo;

    @Override
    public List<TradeEntity> selectTradeDetail() {
        List<TradeEntity> tradeEntity = tradeMapper.getTrades();
        return tradeEntity;
    }

    /**
     * @param
     * @param
     * @return
     * @Author zikang.yao
     */
    @Override
    public List<OrderShowDTO> searchOrderDetailsByPage(Integer pageNum, Integer pageSize) {
        List<OrderShowDTO> allOrderDetails = lltorderMapper.getAllOrderDetails();
        List<BigInteger> driverIds = lltorderMapper.getAllDriverId();
        List<Long> shipperIds = lltorderMapper.getAllShipperId();
        //先查三张表再利用rpc查司机、货主
        com.ymm.driver.api.utils.Result<List<DriverEntity>> driverInfoResult =
                driverRelatedRepo.batchGetDriverInfo(driverIds);
        Result<List<OrderShipperDTO>> shipperInfoResult = shipperRelatedRepo.batchGetShipperInfo(shipperIds);
        List<DriverEntity> driverInfo = driverInfoResult.getData();
        List<OrderShipperDTO> shipperInfo = shipperInfoResult.getData();
        for (int i = 0; i < allOrderDetails.size(); i++) {
            allOrderDetails.get(i).setDriverPhone(driverInfo.get(i).getDriverPhone());
            allOrderDetails.get(i).setDriverName(driverInfo.get(i).getDriverName());
            allOrderDetails.get(i).setDriverSex(driverInfo.get(i).getDriverSex());
            allOrderDetails.get(i).setDriverType(driverInfo.get(i).getDriverType());
            allOrderDetails.get(i).setShipperName(shipperInfo.get(i).getNickname());
            allOrderDetails.get(i).setShipperPhone(shipperInfo.get(i).getPhoneNumber());
        }
        List<OrderShowDTO> pageInfoList = getPageInfo(allOrderDetails, pageNum, pageSize);
        return pageInfoList;
    }

    /**
     * @param allOrderList
     * @param pageNum
     * @param pageSize
     * @return
     * @Author zikang.yao
     * 分页方法
     */
    public static <T> List<T> getPageInfo(List<T> allOrderList, int pageNum, int pageSize) {
        List<T> orderPageList = new ArrayList<>();
        if (allOrderList != null && allOrderList.size() > 0) {
            int currIdx = (pageNum > 1 ? (pageNum - 1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < allOrderList.size() - currIdx; i++) {
                orderPageList.add(allOrderList.get(currIdx + i));
            }
        }
        return orderPageList;
    }


//    @Override
//    public List<OrderShowDTO> searchOrderDetailsByPage(Integer pageNum, Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<LltorderDetailsDTO> lltorderDetailsDTOS = lltorderMapper.listLltorderDetails();
//        for (LltorderDetailsDTO lltorderDetailsDTO: lltorderDetailsDTOS) {
//            lltorderDetailsDTO.setShipperEntity(null);
//            lltorderDetailsDTO.setDriverEntity(null);
//        }
//        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
//        for(int i = 0; i < pageSize; i++){
//            OrderShowDTO orderShowDTO = new OrderShowDTO();
//            BeanUtils.copyProperties(lltorderDetailsDTOS.get(i), orderShowDTO);
//            orderShowDTOS.add(orderShowDTO);
//        }
//        return orderShowDTOS;
//    }

    @Override
    public List<LltorderDetailsDTO> searchOrderDetails() {
        List<LltorderDetailsDTO> lltorderDetailsDTOS = lltorderMapper.listLltorderDetails();
        for (LltorderDetailsDTO lltorderDetailsDTO : lltorderDetailsDTOS) {
            com.ymm.driver.api.utils.Result<DriverEntity> driverInfoResult = driverRelatedRepo.getDriverInfo(BigInteger.valueOf(lltorderDetailsDTO.getDriverId()));
            Result<OrderShipperDTO> shipperInfoResult = shipperRelatedRepo.getShipperInfo(lltorderDetailsDTO.getShipperId());
            com.ymm.system.api.constants.DriverEntity driverEntity = new com.ymm.system.api.constants.DriverEntity();
            if (driverInfoResult.getData() != null) {
                BeanUtils.copyProperties(driverInfoResult.getData(), driverEntity);
                driverEntity.setDriverId(Long.valueOf(String.valueOf(driverInfoResult.getData().getDriverId())));
            }
            lltorderDetailsDTO.setDriverEntity(driverEntity);
            ShipperEntity shipperEntity = new ShipperEntity();
            if (shipperInfoResult.getData() != null) {
                BeanUtils.copyProperties(shipperInfoResult.getData(), shipperEntity);
            }
            lltorderDetailsDTO.setShipperEntity(shipperEntity);
        }
        return lltorderDetailsDTOS;
    }

    @Override
    public List<OrderShowDTO> searchOrderBycondition(Integer pageNumber, Integer pageSize, QueryOrderDTO queryOrderDTO) {
        return null;
    }

    /**
     * @author zikang.yao
     * @param driverGrabDTO
     * @return
     */
    @Override
    public com.ymm.system.api.utils.Result<CargoInfoDto> driverSearchCargoDetails(DriverGrabDTO driverGrabDTO) {
        CargoInfoDto cargoInfoDto = new CargoInfoDto();
        /**
         * 时间校验
         */
        long nd = 1000 * 60 * 60 * 24;//算天
        long nh = 1000 * 60 * 60;//算小时
        long nm = 1000 * 60;//算分钟
        Boolean isAllowedGrab = false;
        CargoEntity cargoEntity = cargoMapper.getCargoDetailsById(driverGrabDTO.getCargoId());
        Date currentCargoDeliverTime = cargoEntity.getCargoDeliveryTime();
        //根据司机Id查找进行中订单
        List<Date> onShelfOrderDate = lltorderMapper.getOnShelfOrderDate(driverGrabDTO.getDriverId());
        if (onShelfOrderDate.size() != 0) {
            for (Date date : onShelfOrderDate) {
                /**
                 * 当前货源时间是否和进行中的订单的时间前后冲突？
                 */
                //当前订单时间-当前货源时间
                long diffH = Math.abs(date.getTime() - currentCargoDeliverTime.getTime()) / nm;
                System.out.println("时间差"+diffH);
                if (diffH > 120) {
                    isAllowedGrab = true;
                } else {
                    isAllowedGrab = false;
                    break;
                }
            }
        } else {
            isAllowedGrab = true;
        }

        if (!isAllowedGrab) {
            cargoInfoDto.setCargoSrc(cargoEntity.getCargoSrc());
            cargoInfoDto.setCargoSrcProv(cargoEntity.getCargoSrcProv());
            cargoInfoDto.setCargoSrcCity(cargoEntity.getCargoSrcCity());
            cargoInfoDto.setCargoSrcRegion(cargoEntity.getCargoSrcRegion());
//            cargoInfoDto.setCargoSrcLat(new BigDecimal(cargoEntity.getCargoSrcLat()));
//            cargoInfoDto.setCargoSrcLon(new BigDecimal(cargoEntity.getCargoSrcLon()));
            cargoInfoDto.setCargoDes(cargoEntity.getCargoDes());
            cargoInfoDto.setCargoDesProv(cargoEntity.getCargoDesProv());
            cargoInfoDto.setCargoDesCity(cargoEntity.getCargoDesCity());
            cargoInfoDto.setCargoDesRegion(cargoEntity.getCargoDesRegion());
            cargoInfoDto.setCargoWeight(cargoEntity.getCargoWeight());
            cargoInfoDto.setCargoDescription(cargoEntity.getCargoDescription());
            cargoInfoDto.setCargoRequireType(cargoEntity.getCargoRequireType());
            cargoInfoDto.setCargoDeliveryTime(cargoEntity.getCargoDeliveryTime());
            cargoInfoDto.setCargoPrice(cargoEntity.getCargoPrice());
            cargoInfoDto.setCargoState(cargoEntity.getCargoState());
            cargoInfoDto.setCargoPersonNum(cargoEntity.getCargoPersonNum());
            cargoInfoDto.setCargoComment(cargoEntity.getCargoComment());
            cargoInfoDto.setCargoShipperPhone(cargoEntity.getCargoShipperPhone());
            cargoInfoDto.setCargoReciverPhone(cargoEntity.getCargoReceiverPhone());
            cargoInfoDto.setCargoType(cargoEntity.getCargoType());
            cargoInfoDto.setCargoDistance(cargoEntity.getCargoDistance());
            cargoInfoDto.setWarningMessage("您现在已经有进行/待进行订单,请慎重抢单，以免时间冲突！");
        } else {
            BeanUtils.copyProperties(cargoEntity,cargoInfoDto);
            cargoInfoDto.setWarningMessage("您当前无订单与该货源时间冲突,去抢单吧！");
        }
        return com.ymm.system.api.utils.Result.success(cargoInfoDto);
    }
}
